package edu.gwu.computerarchitecture.operation.execute;

import edu.gwu.computerarchitecture.alu.ALU;
import edu.gwu.computerarchitecture.cpu.CPUUnits;
import edu.gwu.computerarchitecture.entity.Register;
import edu.gwu.computerarchitecture.entity.Register32Bits;
import edu.gwu.computerarchitecture.entity.WordType;
import edu.gwu.computerarchitecture.operation.Instruction;

/**
 * 
 * implement the execution of FADD instruction
 *
 * <p>detailed comment
 * @author Erzheng Dai 2013年10月11日
 * @see Execute
 * @since 1.0
 * @version 1.0
 */
public class ExecuteFADD extends Execute
{
    public ExecuteFADD(CPUUnits units, Instruction instr)
    {
        super(units, instr);
        // TODO Auto-generated constructor stub
    }
    /**
     * 
     * implement the FADD
     * 
     * @see edu.gwu.computerarchitecture.operation.execute.Execute#execute()
     */
    public void execute(){
        float temp1 = 0;
        float temp2 = 0;
        int temp = 0;
        Register32Bits rsr1 = frfRegister(instruction.rsr1);
        temp1 = Float.intBitsToFloat(rsr1.getRegister());
    	calculateEA();
        temp = cpuunits.mdr.getRegister().getWord();
        //get the +1 address content
        cpuunits.mar.setRegister(new WordType(cpuunits.mar.getRegister().getWord()+1));
        cpuunits.mdr.setRegister(new WordType(cpuunits.memory.getMemory(cpuunits.mar.getRegister().getWord())));
        
        temp2 = Float.intBitsToFloat((temp & 0x0000ffff) | ((cpuunits.mdr.getRegister().getWord() & 0x0000ffff) << 16));
        alu = new ALU(temp1, temp2, cpuunits);
        alu.fadd();
        temp = Float.floatToIntBits(alu.getFResult());
        System.out.printf("%f + %f = %f", temp1, temp2, alu.getFResult());
        instruction.dataToRegisterLow = (temp & 0xffff);
        instruction.dataToRegister = (temp >>> 16);
        
    }
}